namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0013. 罗马数字转整数")]
    public class No0013_RomanToInt : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(RomanToInt, "III", 3);
            Assert.TestExe(RomanToInt, "IV", 4);
        }

        public int RomanToInt(string s)
        {
            int getIntValue(char ch)
            {
                switch (ch)
                {
                    case 'I': return 1;
                    case 'V': return 5;
                    case 'X': return 10;
                    case 'L': return 50;
                    case 'C': return 100;
                    case 'D': return 500;
                    case 'M': return 1000;
                    default: return 0;
                }
            }

            int ans = 0;
            int len = s.Length;
            int curr = 0, pre = 0;
            for (int i = len - 1; i >= 0; i--)
            {
                curr = getIntValue(s[i]);
                if (curr >= pre)
                {
                    ans += curr;
                }
                else
                {
                    ans -= curr;
                }
                pre = curr;
            }
            return ans;
        }
    }
}
